{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 正确的log输出 60分。\n",
    "- 解释为什么这里的感知器代码无法完成异或功能 40分。·"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 真实值\n",
    "samples_and = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 0],\n",
    "    [0, 1, 0],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "\n",
    "samples_or = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 1],\n",
    "]\n",
    "\n",
    "\n",
    "samples_xor = [\n",
    "    [0, 0, 0],\n",
    "    [1, 0, 1],\n",
    "    [0, 1, 1],\n",
    "    [1, 1, 0],\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def perceptron(samples):\n",
    "    w = np.array([1, -1]) # 初始的权重\n",
    "    b = 0 # 初始的偏差\n",
    "    a = 1 # 学习率\n",
    "\n",
    "    for i in range(10):\n",
    "        for j in range(4):\n",
    "            x = np.array(samples[j][:2]) #输入的两个数字\n",
    "            y = 1 if np.dot(w, x) + b > 0 else 0 # 逻辑判断，大于0为1，否则为0\n",
    "            d = np.array(samples[j][2]) # 真值\n",
    "\n",
    "            delta_b = a*(d-y) # b的惩罚\n",
    "            delta_w = a*(d-y)*x # w的偏量\n",
    "\n",
    "\n",
    "            print('epoch {} sample {} \\n [w0: {} w1: {} b: {} \\n x:{} y: {} \\n'\n",
    "                  ' delta_w0 {} delta_w1 {} delta_b {}]'.format(\n",
    "                i, j, w[0], w[1], b,x, y, delta_w[0], delta_w[1], delta_b\n",
    "            ))\n",
    "            w = w + delta_w\n",
    "            b = b + delta_b\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 0 sample 1 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 -1 delta_w1 0 delta_b -1]\n",
      "epoch 0 sample 2 \n",
      " [w0: 0 w1: -1 b: -1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 0 sample 3 \n",
      " [w0: 0 w1: -1 b: -1 \n",
      " x:[1 1] y: 0 \n",
      " delta_w0 1 delta_w1 1 delta_b 1]\n",
      "epoch 1 sample 0 \n",
      " [w0: 1 w1: 0 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 1 sample 1 \n",
      " [w0: 1 w1: 0 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 -1 delta_w1 0 delta_b -1]\n",
      "epoch 1 sample 2 \n",
      " [w0: 0 w1: 0 b: -1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 1 sample 3 \n",
      " [w0: 0 w1: 0 b: -1 \n",
      " x:[1 1] y: 0 \n",
      " delta_w0 1 delta_w1 1 delta_b 1]\n",
      "epoch 2 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 2 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 -1 delta_w1 0 delta_b -1]\n",
      "epoch 2 sample 2 \n",
      " [w0: 0 w1: 1 b: -1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 2 sample 3 \n",
      " [w0: 0 w1: 1 b: -1 \n",
      " x:[1 1] y: 0 \n",
      " delta_w0 1 delta_w1 1 delta_b 1]\n",
      "epoch 3 sample 0 \n",
      " [w0: 1 w1: 2 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 3 sample 1 \n",
      " [w0: 1 w1: 2 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 -1 delta_w1 0 delta_b -1]\n",
      "epoch 3 sample 2 \n",
      " [w0: 0 w1: 2 b: -1 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 -1 delta_b -1]\n",
      "epoch 3 sample 3 \n",
      " [w0: 0 w1: 1 b: -2 \n",
      " x:[1 1] y: 0 \n",
      " delta_w0 1 delta_w1 1 delta_b 1]\n",
      "epoch 4 sample 0 \n",
      " [w0: 1 w1: 2 b: -1 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 4 sample 1 \n",
      " [w0: 1 w1: 2 b: -1 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 4 sample 2 \n",
      " [w0: 1 w1: 2 b: -1 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 -1 delta_b -1]\n",
      "epoch 4 sample 3 \n",
      " [w0: 1 w1: 1 b: -2 \n",
      " x:[1 1] y: 0 \n",
      " delta_w0 1 delta_w1 1 delta_b 1]\n",
      "epoch 5 sample 0 \n",
      " [w0: 2 w1: 2 b: -1 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 5 sample 1 \n",
      " [w0: 2 w1: 2 b: -1 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 -1 delta_w1 0 delta_b -1]\n",
      "epoch 5 sample 2 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 5 sample 3 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 0 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 1 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 2 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 3 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 0 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 1 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 2 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 3 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 0 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 1 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 2 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 3 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 0 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 1 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 2 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 3 \n",
      " [w0: 1 w1: 2 b: -2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n"
     ]
    }
   ],
   "source": [
    "print('logical and')\n",
    "perceptron(samples_and)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 根据感知机计算出的结果与真实值的偏差纠正权重w，偏量b\n",
    "- 在epoch5的时候稳定 $w_0 = 1, w_1 = 2, b = -2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical or\n",
      "epoch 0 sample 0 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 0 sample 1 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 0 sample 2 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 0 sample 3 \n",
      " [w0: 1 w1: 0 b: 1 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 1 sample 0 \n",
      " [w0: 1 w1: 0 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 1 sample 1 \n",
      " [w0: 1 w1: 0 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 1 sample 2 \n",
      " [w0: 1 w1: 0 b: 0 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 1 sample 3 \n",
      " [w0: 1 w1: 1 b: 1 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 2 sample 0 \n",
      " [w0: 1 w1: 1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 2 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 2 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 2 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 3 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 3 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 3 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 3 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 4 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 4 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 4 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 4 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 5 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 5 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 5 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 5 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 6 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 7 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 8 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 0 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 1 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 2 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[0 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 9 sample 3 \n",
      " [w0: 1 w1: 1 b: 0 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n"
     ]
    }
   ],
   "source": [
    "print('logical or')\n",
    "perceptron(samples_or)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 根据感知机计算出的结果与真实值的偏差纠正权重w，偏量b\n",
    "- 在epoch2的时候稳定 $w_0 = 1, w_1 = 1, b = 0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical xor\n",
      "epoch 0 sample 0 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 0 sample 1 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[1 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 0 sample 2 \n",
      " [w0: 1 w1: -1 b: 0 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 0 sample 3 \n",
      " [w0: 1 w1: 0 b: 1 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 1 sample 0 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[0 0] y: 0 \n",
      " delta_w0 0 delta_w1 0 delta_b 0]\n",
      "epoch 1 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 1 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 1 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 2 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 2 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 2 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 2 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 3 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 3 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 3 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 3 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 4 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 4 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 4 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 4 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 5 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 5 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 5 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 5 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 6 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 6 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 6 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 6 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 7 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 7 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 7 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 7 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 8 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 8 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 8 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 8 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n",
      "epoch 9 sample 0 \n",
      " [w0: 0 w1: -1 b: 1 \n",
      " x:[0 0] y: 1 \n",
      " delta_w0 0 delta_w1 0 delta_b -1]\n",
      "epoch 9 sample 1 \n",
      " [w0: 0 w1: -1 b: 0 \n",
      " x:[1 0] y: 0 \n",
      " delta_w0 1 delta_w1 0 delta_b 1]\n",
      "epoch 9 sample 2 \n",
      " [w0: 1 w1: -1 b: 1 \n",
      " x:[0 1] y: 0 \n",
      " delta_w0 0 delta_w1 1 delta_b 1]\n",
      "epoch 9 sample 3 \n",
      " [w0: 1 w1: 0 b: 2 \n",
      " x:[1 1] y: 1 \n",
      " delta_w0 -1 delta_w1 -1 delta_b -1]\n"
     ]
    }
   ],
   "source": [
    "print('logical xor')\n",
    "perceptron(samples_xor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 根据感知机计算出的结果与真实值的偏差纠正权重w，偏量b\n",
    "- 到最后一个迭代依然无法收敛\n",
    "### 感知机无法满足异或功能的原因：\n",
    "本例中，模型仅有一层感知机，即一层权重与偏度，计算$y=w_1x_1+w_2x_w$，进而通过计算y的阶跃函数，得到结果（1或0）。这相当于在一个二维平面中，赋予$(1,1), (1,0), (0,1), (0,0)$四个点四个值，然后以一条直线将平面分成结果为1和为0的两部分。\n",
    "\n",
    "- 与：$(1,1)$均为1， $(0,0), (1,0), (0,1)$为0\n",
    "- 或：$(1,1), (1,0), (0,1)$均为1， $(0,0)$为0\n",
    "以上两种情况均可以得到一条直线将平面分成结果为1和为0的两部分。\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
